package com.hina.rec.endpoint.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.hina.rec.application.service.system.UserService;
import com.hina.rec.common.model.IdReq;
import com.hina.rec.common.model.Result;
import com.hina.rec.endpoint.req.system.UserPageReq;
import com.hina.rec.endpoint.req.system.UserSaveReq;
import com.hina.rec.endpoint.req.system.UserSearchReq;
import com.hina.rec.endpoint.req.system.UserUpdateReq;
import com.hina.rec.endpoint.res.base.PageRes;
import com.hina.rec.endpoint.res.system.UserRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户管理接口
 *
 * @author liaosp
 * @date 2024-01-10
 */
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@SaIgnore
public class UserController {

    private final UserService userService;

    @SaCheckPermission(value = "system:user:edit")
    @PostMapping("/save")
    @Operation(summary = "创建用户")
    public Result<Boolean> save(@RequestBody @Valid UserSaveReq req) {
        return Result.success(userService.save(req));
    }

    @SaCheckPermission(value = "system:user:edit")
    @PostMapping("/update")
    @Operation(summary = "更新用户")
    public Result<Boolean> update(@RequestBody @Valid UserUpdateReq req) {
        return Result.success(userService.update(req));
    }

    @SaCheckPermission(value = "system:user:edit")
    @PostMapping("/delete")
    @Operation(summary = "删除用户")
    public Result<Boolean> delete(@RequestBody @Valid IdReq req) {
        return Result.success(userService.delete(req.getId()));
    }

    @SaCheckPermission(value = "system:user:query")
    @PostMapping("/page")
    @Operation(summary = "分页查询用户")
    public Result<PageRes<UserRes>> page(@RequestBody @Valid UserPageReq req) {
        return Result.success(userService.page(req));
    }

    @SaCheckPermission(value = "system:user:query")
    @PostMapping("/search")
    @Operation(summary = "搜索用户")
    public Result<List<UserRes>> search(@RequestBody @Valid UserSearchReq req) {
        return Result.success(userService.search(req));
    }

    @SaCheckPermission(value = "system:user:query")
    @PostMapping("/simple_list")
    @Operation(summary = "获取用户精简信息列表")
    public Result<List<UserRes>> simpleList() {
        return Result.success(userService.simpleList());
    }

    @SaCheckPermission(value = "system:user:query")
    @PostMapping("/detail")
    @Operation(summary = "获取用户详情")
    public Result<UserRes> detail(@RequestBody @Valid IdReq req) {
        return Result.success(userService.detail(req));
    }
}